from requests_html import HTMLSession
import random
import csv
import json
import datetime

session = HTMLSession()
USER_AGENTS = []

def getTime():
	pass

class DxySARI:

	def __init__(self):
		self.dxyurl = ''

		pass

	# 页面数据+目标字符串
	def get_html_page(self):
		response = session.get(self.dxyurl)
		page = response.html.html
		# print(page)
		start = page.find("window.getAreaStat = [")
		# print(start)
		temp = page[start+22:]
		end = temp.find("]}catch(e){}")
		temp = temp[0:end]
		items = temp.split("]},")
		# 最后一个元素,特殊处理(尾部没有"."号)
		last = items[-1]
		items.pop()
		newitems = []
		for item in items:
			item = item + "]}"
			newitems.append(item)
		newitems.append(last)

		self.items = newitems
		return newitems

	# 数据可视化
	def get_detail_info(self, items):
		# 格式转换
		for item in items:
			js = js.loads(item)
			# print(js)
		# 格式转换:json-->字典
			dc = {}
			dc = dict(js)
			# 省份数据（含城市）
			self.province_list.append(dc)
			# 城市数据（前缀增加省份）
			# print(dc["provinceName"])
			cities = dc["cities"]
			# 需要增加判断，处理列表为空的情况(直辖市和特区的问题)
			if cities:
				for city in cities:
					city["provinceName"] = dc["provinceName"]
					self.city_list.append(city)
					# print(city.keys())
		# 保存至文件
		# print(self.province_list)
		# print(self.city_list)